package com.example.myframe.utils;

import android.annotation.SuppressLint;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.blankj.utilcode.util.AppUtils;
import com.example.myframe.R;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.OnDownloadListener;
import com.hjq.http.model.HttpMethod;
import com.a.lib_bean.api.GiteeApi;
import com.a.lib_bean.bean.GiteeApk;
import com.a.lib_network.http.SimpleCallBack;
import com.a.lib_utils.TipUtil;
import com.qmuiteam.qmui.skin.QMUISkinManager;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.popup.QMUIFullScreenPopup;
import com.qmuiteam.qmui.widget.popup.QMUIPopups;

import java.io.File;

public class HttpUtils {



    /**
     * 这个链接为giteeApi，更换你的gitee地址或其他实现方式，目的是为了更新版本
     */
    public static void showUpdateDialog(AppCompatActivity activity, String url, double version, View v) {
        EasyHttp.get(activity).api(new GiteeApi(url))
                .request(new SimpleCallBack<GiteeApk>(activity) {
                    @Override
                    public void succeed(GiteeApk result) {
                        try {
                            if (version < Float.parseFloat(result.getTagName().substring(1))) {//如果版本小于就通知更新
                                new QMUIDialog.MessageDialogBuilder(activity)
                                        .setTitle("版本更新")
                                        .setMessage("当前有新版本，请问是否更新")
                                        .addAction("取消", (dialog, index) -> dialog.dismiss())
                                        .addAction("确定", (dialog, index) -> {
                                            easyDownloadingPopups(activity, result.getTagName(), result.getAssets().get(0).getBrowserDownloadUrl(), v);
                                            dialog.dismiss();
                                        })
                                        .show();
                            }
                        } catch (Exception e) {
                            TipUtil.toast("更新失败");
                        }
                    }
                });
    }

    /**
     * 简易的下载弹窗
     */
    public static void easyDownloadingPopups(AppCompatActivity activity, String fileName, String url, View v) {
        final View inflate = LayoutInflater.from(activity).inflate(R.layout.dialog_downloading, null, true);
        TextView tv = inflate.findViewById(R.id.tv_popup_download_num);
        ProgressBar pg = inflate.findViewById(R.id.pg_popup_download);
        final QMUIFullScreenPopup qmuiFullScreenPopup = QMUIPopups.fullScreenPopup(activity)
                .addView(inflate)
                .closeBtn(false)
                .skinManager(QMUISkinManager.defaultInstance(activity));
//                .onBlankClick(popup -> Toast.makeText(activity, "点击到空白区域", Toast.LENGTH_SHORT).show())
//                .onDismiss(() -> Toast.makeText(activity, "onDismiss", Toast.LENGTH_SHORT).show());
        qmuiFullScreenPopup.show(v);
        EasyHttp.download(activity)
                .method(HttpMethod.GET)
                .file(new File(Environment.getExternalStorageDirectory(), "c_" + fileName + ".apk"))
                .url(url)
//                .md5("2E8BDD7686474A7BC4A51ADC3667CABF")//用来判断是否重复下载
                .listener(new OnDownloadListener() {

                    @Override
                    public void onStart(File file) {
                    }

                    @SuppressLint("SetTextI18n")
                    @Override
                    public void onProgress(File file, int progress) {
                        pg.setProgress(progress);
                        tv.setText(progress + "%");
                    }

                    @Override
                    public void onComplete(File file) {
                        qmuiFullScreenPopup.dismiss();
                        TipUtil.toast("下载完成：" + file.getPath());
                        AppUtils.installApp(file);
                    }

                    @Override
                    public void onError(File file, Exception e) {
                        qmuiFullScreenPopup.dismiss();
                        TipUtil.toast("下载出错：" + e.getMessage());
                    }

                    @Override
                    public void onEnd(File file) {
                        TipUtil.dismiss();
                    }

                }).start();
    }
}
